package com.library.web.controller.picture;

import com.library.common.annotation.Log;
import com.library.common.core.controller.BaseController;
import com.library.common.core.domain.AjaxResult;
import com.library.common.core.page.PageDomain;
import com.library.common.core.page.TableDataInfo;
import com.library.common.core.page.TableSupport;
import com.library.common.enums.BusinessType;
import com.library.common.utils.poi.ExcelUtil;
import com.library.picture.books.entity.PictureType;
import com.library.picture.books.service.IPictureTypeService;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 绘本分类Controller
 *
 * @author ruoyi
 * @date 2022-04-26
 */
@RestController
@RequestMapping("/picture/type")
public class PictureTypeController extends BaseController {
    @Resource
    private IPictureTypeService pictureTypeService;

    /**
     * 查询绘本分类列表
     */
    @PreAuthorize("@ss.hasPermi('picture:type:list')")
    @GetMapping("/list")
    public TableDataInfo list(PictureType pictureType) {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        Pageable pageable = PageRequest.of(pageNum - 1, pageSize, Sort.by(Sort.Direction.DESC, "updateTime"));
        List<PictureType> list = pictureTypeService.selectPictureTypeList(pictureType, pageable);
        return getDataTable(list, pictureType.getTotal());
    }

    /**
     * 导出绘本分类列表
     */
    @PreAuthorize("@ss.hasPermi('picture:type:export')")
    @Log(title = "绘本分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PictureType pictureType) {
        Pageable pageable = PageRequest.of(0, 10, Sort.by(Sort.Direction.DESC, "updateTime"));
        List<PictureType> list = pictureTypeService.selectPictureTypeList(pictureType, pageable);
        ExcelUtil<PictureType> util = new ExcelUtil<PictureType>(PictureType.class);
        util.exportExcel(response, list, "绘本分类数据");
    }

    /**
     * 获取绘本分类详细信息
     */
    @PreAuthorize("@ss.hasPermi('picture:type:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(pictureTypeService.selectPictureTypeById(id));
    }

    /**
     * 新增绘本分类
     */
    @PreAuthorize("@ss.hasPermi('picture:type:add')")
    @Log(title = "绘本分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PictureType pictureType) {
        return toAjax(pictureTypeService.insertPictureType(pictureType));
    }

    /**
     * 修改绘本分类
     */
    @PreAuthorize("@ss.hasPermi('picture:type:edit')")
    @Log(title = "绘本分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PictureType pictureType) {
        return toAjax(pictureTypeService.updatePictureType(pictureType));
    }

    /**
     * 删除绘本分类
     */
    @PreAuthorize("@ss.hasPermi('picture:type:remove')")
    @Log(title = "绘本分类", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(pictureTypeService.deletePictureTypeByIds(ids));
    }
}
